package com.example.rdemo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.rdemo.entity.Goods;
import com.example.rdemo.entity.Shop;
import com.example.rdemo.mapper.GoodsMapper;
import com.example.rdemo.service.GoodsService;
import com.example.rdemo.utils.PageResult;
import com.example.rdemo.utils.PassToken;
import com.example.rdemo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * goodsController
 * 
 * @author ruoyi
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/system/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @PassToken
    //查询goods列表
    @GetMapping("/list")
    public ResponseResult list(Goods goods) {
        System.out.println(goods.getCid());
        List<Goods> list = goodsService.selectGoodsList(goods);
        return ResponseResult.createBySuccess(200,list,"查询成功");
    }
    @PassToken
    //获取goods详细信息
//    @GetMapping(value = "/{gid}")
//    public ResponseResult getInfo(@PathVariable("gid") Long gid)
//    {
//        Goods goods = goodsService.selectGoodsByGid(gid);
//        return ResponseResult.createBySuccess(200,goods,"查询成功");
//    }
    //cid查询
    @GetMapping(value = "/{cid}")
    public ResponseResult selectByCid(@PathVariable("cid") Long cid)
    {
        Goods goods = goodsService.selectByCid(cid);
        return ResponseResult.createBySuccess(200,goods,"查询成功");
    }

    //新增goods
    @PostMapping
    public ResponseResult add(@RequestBody Goods goods) {
        int i = goodsService.insertGoods(goods);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","添加成功");
        }else {
            return ResponseResult.createByFail(500,"添加失败");
        }
    }

    //修改goods
    @PutMapping
    public ResponseResult<? extends Object> edit(@RequestBody Goods goods) {
        int i = goodsService.updateGoods(goods);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","更新成功");
        }else {
            return ResponseResult.createByFail(500,"更新");
        }
    }

    //删除goods
	@DeleteMapping("/{gids}")
    public ResponseResult remove(@PathVariable Long[] gids) {
        int i = goodsService.deleteGoodsByGids(gids);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","删除成功");
        }else {
            return ResponseResult.createByFail(500,"删除失败");
        }
    }
}
